package com.suoluo.zenadmin.service.friend;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.dao.friend.FriendDAO;
import com.suoluo.zenadmin.oss.OSSService;
import com.suoluo.zenadmin.oss.OSSService.OSSImageException;
import com.suoluo.zenadmin.service.ServiceException;
import com.suoluo.zenadmin.vo.friend.FriendItemVO;
import com.suoluo.zenadmin.vo.friend.FriendTypeVO;

public class FriendServiceImpl implements FriendService {

	@Autowired
	private FriendDAO friendDAO;
	@Resource(name = "ossService")
	private OSSService ossService;

	@Override
	public List<FriendTypeVO> findListFriendType() {
		return friendDAO.findListFriendType();
	}

	@Override
	public void saveFriendType(FriendTypeVO typePOJO) {
		friendDAO.saveFriendType(typePOJO);

	}

	@Override
	public FriendTypeVO findFriendTypeById(int typeId) {
		return friendDAO.findFriendTypeById(typeId);
	}

	@Override
	public void updateFriendType(FriendTypeVO typePOJO) {
		friendDAO.updateFriendType(typePOJO);

	}

	@Override
	public void deleteFriendType(int typeId) {
		friendDAO.deleteFriendType(typeId);
	}

	@Override
	public List<FriendItemVO> findListFriendItem(int typeId) {
		return friendDAO.findListFriendItem(typeId);
	}

	@Override
	public void saveFriendItem(FriendItemVO itemVO) {
		String imgName = null;
		String logo = null;
		try {
			imgName = ossService.putImage(itemVO.getMfile());
			itemVO.setFriendOrigImg(imgName);
			int friendTypeId = itemVO.getFriendTypeId();
			if(friendTypeId == 3)
			{
				logo = ossService.putImage(itemVO.getLogoFile());
			}
			itemVO.setIcon(logo);
			friendDAO.saveFriendItem(itemVO);
		} catch (OSSImageException e) {
			throw new ServiceException(CommonResp.FAIL_CODE, "友情链接项图片上传失败！", e);
		} catch (Exception e) {
			ossService.deleteImage(imgName);
			ossService.deleteImage(logo);
			throw new ServiceException(CommonResp.FAIL_CODE, "友情链接项保存失败！", e);
		}

	}

	@Override
	public FriendItemVO findFriendItemById(int itemId) {
		return friendDAO.findFriendItemById(itemId);
	}

	@Override
	public void updateFriendItem(FriendItemVO itemVO) {
		String imgName = null;
		//航空公司的小logo
		String logo = null;
		try {
			String oldImageName = itemVO.getFriendOrigImg();
			MultipartFile mfile = itemVO.getMfile();
			if(mfile != null && !mfile.isEmpty()) {
				imgName = ossService.updateImage(mfile, oldImageName);
				itemVO.setFriendOrigImg(imgName);
			}
			int oldOrder = itemVO.getOldOrder();
			int newOrder = itemVO.getFriendItemOrder();
			// 顺序修改了
			if (oldOrder != newOrder) {
				// 更新同类别下的顺序
				friendDAO.updateItemOrder(itemVO.getFriendTypeId(), oldOrder,
						newOrder);

			}
			MultipartFile logoFile = itemVO.getLogoFile();
 			if(logoFile != null && !logoFile.isEmpty()) {
				logo = ossService.updateImage(logoFile, itemVO.getIcon());
			}
 			itemVO.setIcon(logo);
			friendDAO.updateFriendItem(itemVO);
		} catch (OSSImageException e) {
			throw new ServiceException(CommonResp.FAIL_CODE, "友情链接项logo更新失败！",e);
		}catch (Exception e) {
			ossService.deleteImage(imgName);
			ossService.deleteImage(logo);
			throw new ServiceException(CommonResp.FAIL_CODE, "友情链接项更新失败！",e);
		}
	}

	@Override
	public void deleteFriendItem(FriendItemVO itemVO) {
		// 删除图片
		ossService.deleteImage(itemVO.getFriendOrigImg());
		ossService.deleteImage(itemVO.getIcon());
		// 删除数据
		friendDAO.deleteFriendItem(itemVO.getFriendItemId());
	}

	@Override
	public int findFriendItemMaxOrder() {
		return friendDAO.findFriendItemMaxOrder();
	}

}
